local vpad = {}
local size = 80
function vpad:init()
	local sw,sh = love.graphics.getDimensions()
	--self.scale = sh/WindowMaxSize
	self.left = {x = 20 , y = sh/2 -40,w = size, h = size/2}
	self.right = {x = 140,y = sh/2 -40,w = size, h = size/2}
	self.up = {x = 100,y = sh/2 - 120,w = size/2, h = size}
	self.down = {x = 100,y = sh/2 + 0,w = size/2, h = size}
	self.a = {x = sw - 100-size/2 , y = sh/2 - 100,w = size, h = size}
	self.b = {x = sw - 180-size/2, y = sh/2,w = size, h = size}
	return self
end

function vpad:inRect(b)
	b.down = self.mx>b.x and self.mx<b.x+b.w 
		and self.my>b.y and self.my<b.y+b.h
end

function vpad:reset()
	self.mx,self.my = -1,-1
	self.up.down = false
	self.down.down = false
	self.left.down = false
	self.right.down = false
	self.a.down = false
	self.b.down = false
end

function vpad:update()
	self:reset()
	local touches = love.touch.getTouches()
    for i, id in ipairs(touches) do
        self.mx, self.my = love.touch.getPosition(id)
        self:inRect(self.left)
		self:inRect(self.right)
		self:inRect(self.up)
		self:inRect(self.down)
		self:inRect(self.a)
		self:inRect(self.b)
    end
end

function vpad:control(tank,dt)
	if self.up.down then
        tank.dx = 0
        tank.dy = - tank.speed*dt
        tank.rot = 0
    elseif self.down.down then
        tank.dx = 0
        tank.dy = tank.speed*dt
        tank.rot = 2
    end
    if self.left.down then
        tank.dy = 0
        tank.dx = - tank.speed*dt
        tank.rot = 3
    elseif self.right.down then
        tank.dy = 0
        tank.dx = tank.speed*dt
        tank.rot = 1
    end
    if self.a.down or self.b.down then
    	tank:fire()
    end
end

function vpad:draw()
	love.graphics.setColor(1, 1, 1, 0.6)
	love.graphics.rectangle("line", self.left.x, self.left.y, self.left.w, self.left.h)
	love.graphics.rectangle("line", self.right.x, self.right.y, self.right.w, self.right.h)
	love.graphics.rectangle("line", self.up.x, self.up.y, self.up.w, self.up.h)
	love.graphics.rectangle("line", self.down.x, self.down.y,self.down.w, self.down.h)
	love.graphics.setFont(texture.font)
	--love.graphics.rectangle("line", self.a.x, self.a.y, self.a.w, self.a.h)
	--love.graphics.rectangle("line", self.b.x, self.b.y,self.b.w, self.b.h)

	love.graphics.circle("line", self.a.x+self.a.w/2, self.a.y+self.a.w/2, self.a.w/2)
	love.graphics.circle("line", self.b.x+self.b.w/2, self.b.y+self.b.w/2, self.b.w/2)
	love.graphics.print("A", self.a.x+32, self.a.y+25)
	love.graphics.print("B", self.b.x+32, self.b.y+25)
end

return vpad